package com.aerozhonghuan.oknet2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * OknetHttpUtil
 * Created by zhangyunfei on 16/3/11.
 */
public class OknetHttpUtil {
    private static final int cacheSize = 10 * 1024 * 1024; // 10 MiB
    private static final String TAG = OknetHttpUtil.class.getSimpleName();

    private static OkHttpClient mOkHttpClient;

    static {
        mOkHttpClient = createOkHttpClient();
    }

    public static OkHttpClient createOkHttpClient() {
        if (OknetConfig.getExternalCacheDir() == null)
            throw new RuntimeException("在使用okhttp前，请调用OkHttpConfig.setExternalCacheDir配置缓存目录");
        Cache cache = new Cache(OknetConfig.getExternalCacheDir(), cacheSize);

        OkHttpClient.Builder builder = new OkHttpClient.Builder().
                connectTimeout(30, TimeUnit.SECONDS).
                readTimeout(30, TimeUnit.SECONDS).
                writeTimeout(30, TimeUnit.SECONDS).
                retryOnConnectionFailure(true).
                cache(cache);
        if (OknetConfig.isEnableGzipRequest())
            builder.addNetworkInterceptor(new GzipRequestInterceptor());
        return builder.build();
    }

    /**
     * 创建 call
     * @param request
     * @return
     */
    public static Call newCall(Request request) {
        return mOkHttpClient.newCall(request);
    }

}